﻿
using System.Collections.Generic;
using System.Drawing;
using Silvermoon.Controls.Base;
using Silvermoon.Shapes;
using System;

namespace Silvermoon.Controls.Beta
{
    /// <summary>
    /// Control that displays  derived <see cref="ItemBase"/> controls in  rows with one or more columns, while all items have the same size.
    /// IteGroup is designed to be an item of <see cref="ListBoxBase"/>.
    /// </summary>
    [Obsolete("Consider to replace ListBox with new ListView control.")]
    public class ItemGroup : ItemGroupBase
    {
        #region fields

        private ChromeControl header;
        private bool hideIfEmpty;

        #endregion
        #region ctors/dtors

        /// <summary>
        /// Initializes a new instance of the <see cref="ItemGroup"/> class.
        /// </summary>
        public ItemGroup()
            : base()
        {
            TextColor = Color.White;
            Height = ItemHeight;
        }


        protected override void Dispose(bool dispose)
        {
            if (header != null) header.Dispose();
            header = null;
            base.Dispose(dispose);
        }

        #endregion
        #region props

        public object Tag { get; set; }

        /// <summary>
        /// Gets or sets the background for the header.
        /// </summary>
        public Shape HeaderBackgound
        {
            get
            {
                EnsureHeader();
                return header.Background;
            }
            set
            {
                EnsureHeader();
                header.Background = value;
            }
        }

        /// <summary>
        /// Gets or sets the header to use.
        /// </summary>
        public ChromeControl Header
        {
            get
            {
                EnsureHeader();
                return header;
            }
            set
            {
                if (header != value)
                {
                    Control oldValue = header;
                    header = value;
                    OnHeaderChanged(oldValue);
                }
            }
        }

        /// <summary>
        /// Hides the group in the ListBox if it does not have any items.
        /// </summary>
        public bool HideIfEmpty
        {
            get { return hideIfEmpty; }
            set
            {
                if (hideIfEmpty != value)
                {
                    hideIfEmpty = value;
                    Invalidate(InvalidationFlag.Size);
                }
            }
        }

        #endregion
        #region methods

        protected override int GetTitleHeight()
        {
            return Count > 0 || !hideIfEmpty ? TitleHeight : 0;
        }

        protected override void OnLayout()
        {
            EnsureHeader();
            header.Visible = !string.IsNullOrEmpty(Title);
            if (CheckFlag(InvalidationFlag.Text))
            {
                IGroupHeader h = header as IGroupHeader;
                if (h != null) h.Title = Title;
            }
            if (CheckFlag(InvalidationFlag.Layout))
            {
                header.Bounds = new Rectangle(0, ClipTop, Width, TitleHeight);
            }
            base.OnLayout();
        }

        private void EnsureHeader()
        {
            if (header == null)
            {
                header = CreateHeader();
                VisibleControls.AddLast(header);
            }
        }

        protected virtual ChromeControl CreateHeader()
        {
            return new GroupHeader { Color = Color.FromArgb(24, 24, 24), TextColor = Color.White, Opacity = 255 };
        }

        protected virtual void OnHeaderChanged(Control oldValue)
        {
            if (oldValue != null)
            {
                VisibleControls.Remove(oldValue);
                oldValue.SuggestDispose();
            }
            Control newValue = header;
            if (newValue != null)
            {
                VisibleControls.AddLast(newValue);
            }
            Invalidate(InvalidationFlag.Layout);
        }

        #endregion
    }
}
